import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import stylelint from 'vite-plugin-stylelint';
import eslintPlugin from 'vite-plugin-eslint';
import UnoCSS from 'unocss/vite';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import IconsResolver from 'unplugin-icons/resolver';
import Icons from 'unplugin-icons/vite';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import ElementPlus from 'unplugin-element-plus/vite';
import { viteMockServe } from 'vite-plugin-mock';
import { join } from 'path';
function resolve(dir) {
  return join(__dirname, dir); //可以用process.cwd() path.join()和path.resolve()
}

// https://vite.dev/config/
export default defineConfig(viteEnv => {
  const { VITE_LOCAL_ICON_PREFIX } = viteEnv;

  return {
    plugins: [
      vue(),
      viteMockServe({
        mockPath: 'mock',
        prodEnabled: true
      }),
      UnoCSS(),
      // eslint插件配置
      eslintPlugin({
        include: ['src/**/*.js', 'src/**/*.vue'], // 指定需要检查的文件
        exclude: ['node_modules/**', 'dist/**'], // 指定不需要检查的文件
        fix: false, // 是否自动修复
        cache: true // 是否启用缓存
      }),
      stylelint({
        fix: false,
        include: ['src/**/*.vue', 'src/**/*.css'],
        exclude: ['node_modules/**', 'dist/**']
      }),
      Icons({
        autoInstall: true,
        compiler: 'vue3'
      }),
      AutoImport({
        //自动导入第三方库或组件 不需要手动编写import {xxx} from vue
        // dts: true, // 如果使用 Typescript，需要设置 dts 为 true 插件会在项目根目录生成类型文件 auto-imports.d.ts ，确保该文件在 tsconfig 中被 include
        dts: false,
        imports: ['vue', 'vue-router'],
        eslintrc: {
          enabled: true,
          filepath: './.eslintrc-auto-import.js'
        },
        resolvers: [
          ElementPlusResolver({
            importStyle: false
          }),
          IconsResolver({
            prefix: 'Icon',
            enabledCollections: ['ep']
          })
        ]
      }),
      Components({
        dts: false, // 输出文件，里面都是一些import的组件键值对
        dirs: [], //让src/components里的组件不用再手动引入
        resolvers: [
          ElementPlusResolver({
            importStyle: false
          }),
          IconsResolver({
            prefix: 'Icon',
            enabledCollections: ['ep']
          })
        ]
      }),
      createSvgIconsPlugin({
        iconDirs: [resolve(process.cwd(), 'src/assets/svg')],
        symbolId: `${VITE_LOCAL_ICON_PREFIX}-[dir]-[name]`
      }),
      ElementPlus({
        useSource: true
      })
    ],
    resolve: {
      alias: {
        '@': resolve('/src')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {}
      }
    }
  };
});
